﻿@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="AddComponent-ReagentsId">@ViewData["ReagentsId"]</label>
            <label id="AddComponent-ComponentId">@ViewData["ComponentId"]</label>
            <label id="AddComponent-Parent-ModalID">@ViewData["ModalID"]</label>
            <label id="AddComponent-Return-Value"></label>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
            </div>
            <div class="col-9 pl-0">
                <input id="AddComponent-Name" class="form-control font-rfs border-radius-noleft" />
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">配制浓度</span>
            </div>
            <div class="col-9 pl-0">
                <input id="AddComponent-Concentration" class="form-control font-rfs border-radius-noleft" />
            </div>
        </div>
        <div id="AddComponent-MaterialEmbed" class="input-group"></div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">消耗量*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <input id="AddComponent-Dosage" class="form-control font-rfs border-radius-noleft" style="width:45%" placeholder="有效数字" />
                <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:25%">消耗库存</span>
                <select id="AddComponent-IsInventory" class="form-control font-rfs border-radius-noleft" style="width:30%">
                    <option>否</option>
                    <option>是</option>
                </select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">消耗量单位*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <select id="AddComponent-UnitsType" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
                <select id="AddComponent-Units" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
                <button id="AddComponent-AddUnits" onclick="AddUnits(this)" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">配制方法</span>
            </div>
            <div class="col-9 pl-0">
                <textarea id="AddComponent-PreparationMethod" class="form-control font-rfs border-radius-noleft" style="height: calc(1.5em + 0.75rem + 2px);" placeholder="选填"></textarea>
            </div>
        </div>
    </div>
    <div class="text-right">
        <button id="AddComponent-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="AddComponent-cancel" type="button" class="btn btn-info font-rfs" data-dismiss="modal">取消</button>
    </div>
</div>

<script>

    $(document).ready(function () {
        LoadUnitType();
        LoadComponent();
    });

    function LoadMaterialSelect(MaterialName) {
        $.post("\\Inv\\SelectMaterial", {
            ReturnLabId: "AddComponent-Return-Value",
            MaterialName: MaterialName
        }, function (datas) {
            $("#AddComponent-MaterialEmbed").html(datas);
        });
    }

    function LoadType() {
        $.post("\\Reagent\\GetReagentType", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#AddComponent-Select-Type").append(new Option(data[i].type));
            }
            $("#AddComponent-Select-Type").prepend(new Option("", ""));
            $("#AddComponent-Select-Type").val("");
        });
    }

    function LoadComponent() {
        if ($("#AddComponent-ComponentId").html().length > 0) {
            $.post("\\Reagent\\getComponent", {
                ComponentId: $("#AddComponent-ComponentId").html()
            }, function (data) {
                $("#AddComponent-Name").val(data.name);
                $("#AddComponent-Concentration").val(data.concentration);
                $("#AddComponent-Dosage").val(data.dosage);
                $("#AddComponent-IsInventory").val(data.isInventory == true ? "是" : "否");
                $("#AddComponent-Units").append(new Option(data.dosageUnit));
                $("#AddComponent-PreparationMethod").val(data.preparationMethod);
                $("#AddComponent-Return-Value").attr('data-code', data.materialCode);
                $("#AddComponent-Return-Value").attr('data-name', data.materialName);
                LoadMaterialSelect(data.materialName);
            });
        } else {
            LoadMaterialSelect("");
        }
    }

    function LoadUnitType() {
            $.post("\\Common\\GetUnitType", {
            }, function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#AddComponent-UnitsType").append(new Option(data[i].type));
                }
                $("#AddComponent-UnitsType").prepend(new Option("", ""));
                $("#AddComponent-UnitsType").val("");
            });
        }

    function AddUnits(obj) {
        $("#AddComponent-AddUnits-modalTitle").text("添加单位");

        $("#AddComponent-AddUnits-Modal").modal({
            backdrop: 'static',
            keyboard: false
        });

        $.post("/Common/AddUnit", function (datas) {
            $("#AddComponent-AddUnits-Embed").html(datas);
        });
    }

    $("#AddComponent-UnitsType").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#AddComponent-UnitsType").find("option:selected").text()
        }, function (data) {
            $("#AddComponent-Units").empty();
            for (var i = 0; i < data.length; i++) {
                $("#AddComponent-Units").append(new Option(data[i].code));
            }
        });
    });

    $("#AddComponent-confirm").click(function () {
        if ($.trim($("#AddComponent-Name").val()).length == 0) {
            alert("试剂名称不能为空！");
            $("#AddComponent-Name").addClass('error-style');
            return false;
        }
        if ($.trim($("#AddComponent-Dosage").val()).length == 0) {
            alert("消耗量不能为空！");
            $("#AddComponent-Dosage").addClass('error-style');
            return false;
        }
        if ($("#AddComponent-Units").find("option:selected").text().length == 0) {
            alert("消耗量单位不能为空！");
            $("#AddComponent-Units").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Name"] = $.trim($("#AddComponent-Name").val());
        jsonStr["Concentration"] = $.trim($("#AddComponent-Concentration").val());
        jsonStr["Dosage"] = $.trim($("#AddComponent-Dosage").val());
        jsonStr["DosageUnit"] = $.trim($("#AddComponent-Units").find("option:selected").text());
        jsonStr["MaterialCode"] = $("#AddComponent-Return-Value").attr('data-code');
        jsonStr["MaterialName"] = $("#AddComponent-Return-Value").attr('data-name');
        jsonStr["PreparationMethod"] = $.trim($("#AddComponent-PreparationMethod").val());
        jsonStr["ReagentId"] = $("#AddComponent-ReagentsId").html();

        if ($("#AddComponent-ReagentsId").html().length > 0) {
            AddComponent(jsonStr);
        } else {
            UpdateComponent(jsonStr);
        }
    });

    function AddComponent(jsonStr) {
        $.post("postAddComponent", {
            jsonStr: JSON.stringify(jsonStr),
            IsInventory: $("#AddComponent-IsInventory").find("option:selected").text()
        }, function (data) {
            alert(data);
        });
    }

    function UpdateComponent(jsonStr) {
        jsonStr["Id"] = $("#AddComponent-ComponentId").html();
        $.post("UpdateComponent", {
            jsonStr: JSON.stringify(jsonStr),
            IsInventory: $("#AddComponent-IsInventory").find("option:selected").text()
        }, function (data) {
            alert(data);
            $("#AddComponent-cancel").click();
        });
    }
    
    $("#AddComponent-cancel").click(function () {
        var modalID = $("#AddComponent-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
